package com.webservice.impl;

import com.alibaba.fastjson.JSON;
import com.dao.BiddingDao;
import com.dao.Maintenance_orderDao;
import com.dao.UserDao;
import com.domain.Bidding;
import com.domain.Expense_details;
import com.domain.Maintenance_order;
import com.domain.User;
import com.tool.Until;
import com.webservice.BiddingWebservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebService(targetNamespace = "http://39.98.59.6/mmms_test/ws/bidding")
public class BiddingWebserviceImpl implements BiddingWebservice {

    @Autowired
    private BiddingDao biddingDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private Maintenance_orderDao maintenance_orderDao;

    private Bidding setIcon(Bidding bidding){
        if (bidding != null){
            if (bidding.getCompany_id() != null){
                User user = userDao.findById(bidding.getCompany_id());
                if (user.getIcon() != null){
                    bidding.setCompany_photo(user.getIcon());
                }

            }
        }

        return bidding;
    }

    /**
     * 维修方竞单
     * @param bidding
     */
    @Override
    @WebResult(name = "int")
    public int addBidding(@WebParam(name = "bidding") Bidding bidding) {
        if (bidding != null && bidding.getOrder_id()!=null){
            Maintenance_order order = maintenance_orderDao.findOneById(bidding.getOrder_id());
            order = Until.setOrder(order);

            if (order.getSecond_price() > 0 && bidding.getEstimated_price() > order.getSecond_price()){
                return -3;
            }
            Bidding bidding1 = biddingDao.findBiddingByCompany(bidding.getOrder_id(), bidding.getCompany_id());
            if (bidding1 == null){
                Date date = new Date(System.currentTimeMillis());
                bidding.setCreate_time(date);
                biddingDao.addBidding(bidding);
                if (!StringUtils.isEmpty(bidding.getExpenseDetailsList())){
                    List<Expense_details> expenseDetails = JSON.parseArray(bidding.getExpenseDetailsList(), Expense_details.class);
                    if (expenseDetails!=null){
                        for (Expense_details expense_details1:expenseDetails){
                            maintenance_orderDao.addExpense(expense_details1);
                        }
                    }
                }
            }
            else {
                return -2;
            }

        }
        else {
            return -1;
        }
        return 0;

    }

    @WebResult(name = "BiddingList")
    @Override
    public List<Bidding> findAllByOrder(@WebParam(name = "id") Integer id) {
        List<Bidding> biddingList = biddingDao.findAllByOrder(id);
        List<Bidding> biddingList1 = new ArrayList<>();
        if (biddingList!=null){
            for (Bidding bidding:biddingList){
                bidding = setIcon(bidding);
                List<Expense_details> expense_detailsList = maintenance_orderDao.findExpenseDetails(id, bidding.getCompany_id());
                if (expense_detailsList != null){
                    bidding.setExpenseDetailsList(JSON.toJSONString(expense_detailsList,true));
                }
                biddingList1.add(bidding);
            }
        }

        return biddingList1;

    }

    @Override
    public int updateBidding(@WebParam(name = "bidding") Bidding bidding) {
        if (bidding != null && bidding.getOrder_id()!=null){
            Maintenance_order order = maintenance_orderDao.findOneById(bidding.getOrder_id());
            order = Until.setOrder(order);
            if (order.getSecond_price() > 0 && bidding.getEstimated_price()>order.getSecond_price()){
                return -2;
            }
            List<Expense_details> expense_details = maintenance_orderDao.findExpenseDetails(order.getId(), bidding.getCompany_id());
            if (expense_details!=null){
                maintenance_orderDao.delExpenseDetailsByUnit(order.getId(), bidding.getCompany_id());
            }
            if (!StringUtils.isEmpty(bidding.getExpenseDetailsList())){
                List<Expense_details> expenseDetails = JSON.parseArray(bidding.getExpenseDetailsList(), Expense_details.class);
                if (expenseDetails!=null){
                    for (Expense_details expense_details1:expenseDetails){
                        maintenance_orderDao.addExpense(expense_details1);
                    }
                }
            }
            biddingDao.updateBidding(bidding);
        }
        else {
            return -1;
        }
        return 0;
    }

    @WebResult(name = "Bidding")
    @Override
    public Bidding findBiddingByCompany(@WebParam(name = "order_id")Integer order_id, @WebParam(name = "company_id")Integer company_id) {
        Bidding bidding = biddingDao.findBiddingByCompany(order_id, company_id);
        if (bidding != null){
            bidding = setIcon(bidding);
            List<Expense_details> expense_detailsList = maintenance_orderDao.findExpenseDetails(order_id, bidding.getCompany_id());
            if (expense_detailsList != null){
                bidding.setExpenseDetailsList(JSON.toJSONString(expense_detailsList,true));
            }
            return bidding;
        }else {
            return new Bidding();
        }

    }


    @Override
    public int delByCompanyId(@WebParam(name = "id")Integer id, @WebParam(name = "company_id")Integer company_id) {
        if (id!=null && company_id!=null){
            biddingDao.delByCompany(id, company_id);
        }
        else {
            return -1;
        }
        return 0;
    }
}
